package org.zh.home.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.zh.home.dao.IRoleDao;
import org.zh.home.entity.Resource;
import org.zh.home.entity.Role;
import org.zh.home.entity.RoleResource;
import org.zh.home.model.Operate;
import org.zh.home.model.Pager;
import org.zh.home.model.Query;
import org.zh.home.model.Separator;
import org.zh.home.service.IRoleService;
import org.zh.home.utils.DateUtils;
import org.zh.home.utils.StringUtils;

@Service
public class RoleService implements IRoleService {
	private IRoleDao roleDao;
	
	public IRoleDao getRoleDao() {
		return roleDao;
	}

	@Inject
	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Override
	public void add(Role role,String resources) {
		if(StringUtils.isBlank(role.getName())) throw new RuntimeException("角色名称不能为空");
		String date = DateUtils.date2str(new Date(),"yyyy-MM-dd");
		role.setDate(date);
		role = roleDao.add(role);
		if(StringUtils.isNotBlank(resources)) {
			String[] rs = resources.trim().split(",");
			for(int i=0;i<rs.length;i++) {
				String object = rs[i];
				String[] authority = object.trim().split(":");
				int rid= Integer.parseInt(authority[0] == null?"0":authority[0]);
				Resource resource = (Resource) roleDao.loadEntity(Resource.class,rid);
				if(resource == null) continue;
				RoleResource rr = new RoleResource();
				rr.setResourceId(rid);
				rr.setResourceName(resource.getName());
				rr.setRoleId(role.getId());
				rr.setRoleName(role.getName());
				rr.setAuthority(authority[1]);
				roleDao.addEntity(rr);
			}
		}
	}

	@Override
	public void delete(int id) {
		Role role = roleDao.load(id);
		if(role == null) throw new RuntimeException("要删除的角色不存在");
		roleDao.deleteRoleResource(id);
		roleDao.updateUserGroupRole(id);
		roleDao.delete(role);
	}

	@Override
	public void update(Role role,String resources) {
		if(StringUtils.isBlank(role.getName())) throw new RuntimeException("角色名称不能为空");
		Role temp = roleDao.load(role.getId());
		temp.setName(role.getName());
		roleDao.update(temp);
		//更新角色资源需先删除  、、
		roleDao.deleteRoleResource(temp.getId());
		if(StringUtils.isNotBlank(resources)) {
			String[] rs = resources.trim().split(",");
			for(int i=0;i<rs.length;i++) {
				String object = rs[i];
				String[] authority = object.trim().split(":");
				int rid= Integer.parseInt(authority[0] == null?"0":authority[0]);
				Resource resource = (Resource) roleDao.loadEntity(Resource.class,rid);
				if(resource == null) continue;
				RoleResource rr = new RoleResource();
				rr.setResourceId(rid);
				rr.setResourceName(resource.getName());
				rr.setRoleId(role.getId());
				rr.setRoleName(role.getName());
				rr.setAuthority(authority[1]);
				roleDao.addEntity(rr);
			}
		}
	}

	@Override
	public Role load(int id) {
		return roleDao.load(id);
	}

	@Override
	public Pager<Role> find(String name) {
		List<Query> model = new ArrayList<Query>();
		if(StringUtils.isNotBlank(name)) {
			Query nq = new Query();
			nq.setName("name");
			nq.setOperate(Operate.EQ);
			nq.setSeparator(Separator.AND);
			nq.setValue(name);
			model.add(nq);
		}
		return roleDao.find(model);
	}

}
